package yf.mall.service.impl;

import com.yf.entity.WxMsg;
import com.yf.enums.WxMsgEvent;
import com.yf.enums.WxMsgType;
import com.yf.util.WxUtil;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import yf.mall.service.WxMsgService;

@Log
@Service
public class WxMsgServiceImpl implements WxMsgService {

    @Autowired
    private UserService userService;

    @Override
    public String handle(String body) {
        WxMsg wxMsg = WxUtil.getObjectFromXML(body,WxMsg.class);
        if(wxMsg.getMsgType().equals(WxMsgType.TEXT.getName())){
            log.info("收到文字消息:"+wxMsg.getContent());
        }else if (wxMsg.getMsgType().equals(WxMsgType.EVENT.getName())){
            log.info("扫码关注:"+wxMsg.getContent());
            //扫码事件 注册用户
            if(wxMsg.getEvent().equals(WxMsgEvent.SCAN.getName())||wxMsg.getEvent().equals(WxMsgEvent.SUBSCRIBE.getName())){
                    String openId = wxMsg.getFromUserName();
                    String scene = wxMsg.getEventKey().replace("qrscene_", "");
                    Long topUserId = Long.parseLong(scene);
                    userService.register(openId,topUserId);
            }
        }

        return "";
    }

    public static void main(String[] args) {
        String tt = "33";
        System.out.println(Long.parseLong("44"));
    }

}
